<template>
  <div>
    <el-form>
      <el-form-item lalel="头像">
        <div class="avator">
          <el-upload
            class="avatar-uploader"
            action="http://10.9.41.221:7000/upload/img"
            name="kelin-file"
            :headers="token"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="用户名">
        <el-col :span="6">
          <el-input
            placeholder="username"
            v-model="form.username"
            disabled
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio label="男" v-model="form.sex">男</el-radio>
        <el-radio label="女" v-model="form.sex">女</el-radio>
        <el-radio label="保密" v-model="form.sex">保密</el-radio>
      </el-form-item>
      <el-form-item label="个人简介">
        <el-col>
          <el-input
            type="textarea"
            row="5"
            placeholder="个人简介："
            v-model="form.desc"
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="手机">
        <el-col :span="6">
          <el-input placeholder="phone" v-model="form.phone"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-col :span="6">
          <el-input placeholder="email" v-model="form.email"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="success" round @click="save">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style>
/* .el-button {
  color: #fff;
} */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<script>
export default {
  data() {
    return {
      // token: {
      //   authorization:
      //     "Bearer " +
      //     "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IueZvei1tyIsIl9pZCI6IjYyMGExZDcyYTBlYWQ1OGUwNmI1ODllMiIsImlhdCI6MTY0NTY3OTM2NSwiZXhwIjoxNjQ2Mjg0MTY1fQ.QbXIaLYTjoby4eHKBIzmxXaSbaRW2uDO219tuoMqlVE",
      // },
      form: {
        _id: "",
        sex: "男",
        avatar: "",
        phone: "",
        email: "",
        desc: "",
      },
    };
  },
  computed: {
    token() {
      const cookie = document.cookie.match(/user=\{(.*?)\}/)[0].split('=')[1];
      let user;
      if (cookie) {
        try {
          user = JSON.parse(cookie);
        } catch (error) {
          console.error(error);
        }
      }
      return {
        authorization: "Bearer " + user.token,
      };
    },
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.form.avatar = URL.createObjectURL(file.raw);
      this.form.avatar = res.path;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!(isJPG || isPNG)) {
        this.$message.error("上传头像图片只能是 JPG 或 PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    save() {
      this.$http({
        path: "/accounts/uppsn",
        method: "post",
        params: this.form,
      }).then((res) => {
        if (res.code == 200) {
          this.$store.commit("setStoreUser", this.form);
          this.$message.success("更改成功！");
        }
      });
    },
  },
  created() {
    this.form = this.$store.state.user;
  },
};
</script>